﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.ComponentModel;

[assembly: System.Web.UI.WebResource("Kevin.Controls.Button.Button.js", "text/javascript")]
namespace Kevin.Controls
{
    [RequiredScript(typeof(ModalPopupExtender))]
    [ClientScriptResource("Kevin.Controls.Button", "Kevin.Controls.Button.Button.js")]
    public class Button : System.Web.UI.WebControls.Button, IScriptControl
    {
        private ScriptManager sm;
        protected override void OnPreRender(EventArgs e)
        {
            if (!this.DesignMode)
            {
                // Test for ScriptManager and register if it exists
                sm = ScriptManager.GetCurrent(Page);

                if (sm == null)
                    throw new HttpException("页面中必须有ScriptManager控件。");

                sm.RegisterScriptControl(this);
            }

            base.OnPreRender(e);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
                sm.RegisterScriptDescriptors(this);

            base.Render(writer);
        }
        
        /// <summary>
        /// 提交时页面等待信息
        /// </summary>
        [Description("提交时页面等待信息")]
        public string WaitingMessage
        {
            get;
            set;
        }

        /// <summary>
        /// 提交时确认信息,只有设置了此属性才弹出确认窗口
        /// </summary>
        [Description("提交时确认信息,只有设置了此属性才弹出确认窗口")]
        public string ConfirmMessage
        {
            get;
            set;
        }

        #region IScriptControl 成员

        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Kevin.Controls.Button", this.ClientID);
            descriptor.AddProperty("waitingMessage", this.WaitingMessage);
            descriptor.AddProperty("confirmMessage", this.ConfirmMessage);
            descriptor.AddProperty("CallbackID", this.UniqueID);  
            return new ScriptDescriptor[] { descriptor };
        }

        public IEnumerable<ScriptReference> GetScriptReferences()
        {
            //throw new NotImplementedException();
            if (this.Visible)
            {
                ScriptReference sref = new ScriptReference("Kevin.Controls.Button.Button.js", GetType().Assembly.FullName);
                return new ScriptReference[] { sref };
            }
            return new ScriptReference[] { new ScriptReference() };
        }

        #endregion
    }
}
